package com.example.zxingdemo;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.example.zxingdemo.base.BaseActivity;
import com.king.zxing.lite.Intents;
import com.king.zxing.ui.BJGCaptureActivity;
import com.king.zxing.util.CodeUtils;
import com.king.zxing.util.LayoutUtils;
import com.king.zxing.util.ToastUtil;

import static com.example.zxingdemo.config.RequestCode.CAPTURE_ACTIVITY_REQUEST;

public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";

    private ImageView imageView;
    private Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
    }

    public void button1(View v) {
        startActivityForResult(new Intent(this, BJGCaptureActivity.class), CAPTURE_ACTIVITY_REQUEST);
    }

    public void button2(View v) {
        ToastUtil.show(this, "要使用扫码功能请允许获取相机权限");
    }

    public void button3(View v) {
        String url = "http://weixin.qq.com/r/nD9OVvzEGqFlrbg192qO";
        if (bitmap != null) {
            bitmap.recycle();
        }
        bitmap = CodeUtils.createQRCode(url, LayoutUtils.dpToPx(this,50));
        imageView.setImageBitmap(bitmap);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAPTURE_ACTIVITY_REQUEST && resultCode == Activity.RESULT_OK && data != null) {
            String s = data.getStringExtra(Intents.Scan.RESULT);
//            Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onActivityResult: s:" + s);

        }
    }

    private void test() {

    }
}
